package com.justgame.project.generator.utils;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.google.common.base.CaseFormat;
import com.justgame.project.generator.container.EntityContainer;
import com.justgame.project.generator.properties.GlobalProperties;
import com.justgame.project.generator.schema.Entity;
import org.springframework.context.annotation.DependsOn;
import org.springframework.lang.NonNull;

import java.util.Optional;

@DependsOn(
        value = "entityContainer"
)
public class NameUtils {

    public static EntityContainer entityContainer = AppContextUtils.getBean(EntityContainer.class);

    /**
     * 组装业务模块文件名
     * @param moduleName 模块名
     */
    public static String formatModuleServiceName(String moduleName){
        return String.format("%s-service",moduleName);
    }
    
    /**
     * 组装业务模块文件名
     */
    public static String formatModuleServiceName(TableInfo tableInfo){
        return String.format("%s-service",getModuleName(tableInfo));
    }

    /**
     * 组装业务模块Api文件名
     * @param moduleName 模块名
     */
    public static String formatModuleServiceApiName(String moduleName){
        return String.format("%s-service-api",moduleName);
    }

    /**
     * 首字母大写
     * @param str
     * @return
     */
    public static String firstWorldUpper(@NonNull String str){
        if (str.length() > 1){
            return str.substring(0,1).toUpperCase() + str.substring(1);
        }
        return str.toUpperCase();
    }

    /**
     * 首字母小写
     * @param str
     * @return
     */
    public static String firstWorldLower(@NonNull String str){
        if (str.length() > 1){
            return str.substring(0,1).toLowerCase() + str.substring(1);
        }
        return str.toLowerCase();
    }

    public static String MapperPackage(TableInfo tableInfo){
        GlobalProperties properties = AppContextUtils.getBean(GlobalProperties.class);
        return properties.getOperationPackage().substring(1).replaceAll(StringPool.SLASH, StringPool.DOT) +
               StringPool.DOT + getModuleName(tableInfo) + ".mapper." + NameUtils.firstWorldUpper(tableInfo.getEntityName()) + "Mapper";
    }


    public static String getModuleName(TableInfo tableInfo){
        Optional<Entity> entity = entityContainer.findEntity(ent->ent.getTableName().equals(tableInfo.getName()));
        if (entity.isPresent()){
            return entity.get().getModuleName();
        }
        return getModuleNameByTableName(tableInfo.getName());
    }

    public static String getModuleNameByTableName(String tableName){
        Optional<Entity> entity = entityContainer.findEntity(ent->ent.getTableName().equals(tableName));
        if (entity.isPresent()){
            return entity.get().getModuleName();
        }
        return tableName.split("_")[0];
    }

    public static String getEntityNameByTableName(String tableName){
        return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,tableName);
    }

    public static String getTableNameByEntityName(String entityName){
        return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,entityName);
    }

}
